﻿using DotNetty.Buffers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace AiMei.Redis.Core
{
    public class RedisObject
    {
        public virtual void WriteBuffer(IByteBuffer output)
        {
        }
    }

    public class RedisInterget : RedisObject
    {
        public int Interget { get; set; }
        public override string ToString()
        {
            return Interget.ToString();
        }
    }

    public class ReidsString : RedisObject
    {
        public string Content { get; set; }

        public override void WriteBuffer(IByteBuffer output)
        {

            //output.WriteString(Content, Encoding.UTF8);
            var chars =Content.ToCharArray();
            foreach (var item in chars)
            {
                output.WriteChar(item);
            }
            output.WriteChar('\r');
            output.WriteChar('\n');
        }
        public override string ToString()
        {
            return Content;
        }
    }
    public class SimpleString : ReidsString
    {

        public override void WriteBuffer(IByteBuffer output)
        {
            output.WriteByte((byte)RedisMessageType.SimpleString);
            base.WriteBuffer(output);
        }
    }
    public class MulitString : SimpleString
    {
        public override void WriteBuffer(IByteBuffer output)
        {
            output.WriteByte((byte)RedisMessageType.BulkString);
            base.WriteBuffer(output);
        }
    }

    public class RedisErrorString : SimpleString
    {
        public override void WriteBuffer(IByteBuffer output)
        {
            output.WriteByte((byte)RedisMessageType.Error);
            base.WriteBuffer(output);
        }
    }

    public class RedisCommon : RedisObject
    {
        public Guid RedisId { get; set; }
        public RedisCommon()
        {
            Commond = new List<string>();
            RedisId = Guid.NewGuid();
        }
        public List<string> Commond { get; set; }
        public override void WriteBuffer(IByteBuffer output)
        {
            //请求头部格式， *<number of arguments>\r\n
            //const string headstr = "*{0}\r\n";
            //参数信息       $<number of bytes of argument N>\r\n<argument data>\r\n
            //const string bulkstr = "${0}\r\n{1}\r\n";
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendFormat("*{0}\r\n",Commond.Count);
            foreach (var item in Commond)
            {
                stringBuilder.AppendFormat("${0}\r\n{1}\r\n",item.Length,item);
            }
            //*1\r\n$4\r\nPING\r\n
            byte[] bytes = Encoding.UTF8.GetBytes(stringBuilder.ToString());
            output.WriteBytes(bytes);
        }
    }


    public class RedisArray: RedisObject
    {
        public RedisArray()
        {
            Array = new List<RedisObject>();
        }
        public List<RedisObject> Array { get; set; }
        public override string ToString()
        {
            return string.Join("\r\n", Array);
        }
    }
}
